import Axios, { AxiosResponse, AxiosRequestConfig } from 'axios';
import { Config } from "@tools/Config";

Axios.defaults.baseURL = Config.BASE_URL;

export interface IRequestParam extends AxiosRequestConfig {
    url: string;
    method: string;
}

export async function Request(params: IRequestParam) {
    const method: string = params.method;
    const ip: AxiosRequestConfig = Object.assign({},params,{
        validateStatus: status => {
            return status >= 100 && status <= 505
        }
    });
    if (method.toUpperCase() === 'GET') {
        if (ip.data) {
            delete ip.data;
            ip.params = params.data;
        }
    }
    const result: AxiosResponse = await Axios.request(ip);
    if(result.status === 404) {
        result.data = {
            errorCode: 404,
            errorMessage: `Connot ${ method.toUpperCase() } ${ params.url }`
        };
    }
    return result;
}